﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D2DEngine;
using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

namespace AIDev.Components
{
    public abstract class ProjectileComponent : D2DComponent
    {
        protected D2DActor actor;

        protected bool impulseOnCollision = false;

        protected float damageRadius = 1.0f;
        protected float damageAmount = 0.0f;
        protected float speed = 2.0f;

        public override void OnRegister()
        {
            this.actor = this.owner as D2DActor;
            //this.actor.WorldLimit.OnWorldLimit += OnWorldLimit;
            this.actor.Body.OnCollision += OnCollision;

            base.OnRegister();
        }

        public virtual bool OnCollision(Fixture f1, Fixture f2, Contact contact)
        {
            this.actor.Dispose();
            return true;
        }

        public virtual void OnHit(D2DActor victim)
        {
        }

        public void OnWorldLimit(D2DSceneObject obj)
        {
            obj.Dispose();
        }

        /// <summary>
        /// Updates the projectile
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)(gameTime.ElapsedGameTime.TotalSeconds);

            Vector2 direction = new Vector2((float)Math.Cos(this.actor.Rotation),
                                            (float)Math.Sin(this.actor.Rotation));

            this.actor.Body.ApplyForce(direction * this.speed);
            base.Update(gameTime);
        }
    }
}
